
import { Component,ViewChild  } from '@angular/core';
import { IonicPage, NavController, NavParams, LoadingController, ToastController,Navbar } from 'ionic-angular';
import { ApiProvider } from "../../providers/api/api";
import { DatePipe } from "@angular/common";
import { TeachingPage } from "../teaching/teaching";

/**
 * Generated class for the TeachSignPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-teach-sign',
  templateUrl: 'teach-sign.html',
})
export class TeachSignPage {
  @ViewChild(Navbar) navBar: Navbar;
  number = 10;
  checked = false;
  setsigntime = false;//签到显示
  classcourseId;
  unitId;
  teacherId;
  slides = [];
  total;
  signtext = "签到未进行";
  signminutes;
  signtime;
  skysta;//判断时间是否设置

  constructor(private dataPipe: DatePipe, public loadingCtrl: LoadingController, public navCtrl: NavController, public navParams: NavParams, private toastCtrl: ToastController, public http: ApiProvider) {
  }

  ionViewDidLoad() {
    this.classcourseId = this.navParams.get("classCourseId");
    this.unitId = this.navParams.get("unitId");
    this.teacherId = localStorage.getItem('teacherId');
    this.skysta = localStorage.getItem('skysta');
    this.LoadList(1);
    this.http.post("/startCourseTeacherController/getSignOfTeacher.do", { userId: this.teacherId, unitId: this.unitId }).subscribe(item => {
      this.signtime=item["minutes"]
      if (item["status"] == 1) {
        this.signtext = "签到进行中";
        this.checked = true;
      }else{
        this.checked = false;
      }
    })
  }



  LoadList(e) {
    this.http.post("/startCourseTeacherController/getSignInfoOfTeacher.do", { userId: this.teacherId, unitId: this.unitId, classCourseId: this.classcourseId, rows: 5, page: e }).subscribe(item => {
      item['list'].forEach(e=>{
        e.insert_time.time = this.dataPipe.transform(e.insert_time.time,'yyyy-MM-dd');
      })
      this.slides = item['list']
      console.log(999)
      console.log(item)
      this.total = item['total']
    })
  }
  doSearch(page) {
    this.LoadList(page);
  }
  check(a) {
    if (a&&this.skysta!=1) {
      this.setsigntime = true;
    } else if(!a){
      this.setsigntime = false;
      this.signend();
      this.signtext = "签到未进行";
      this.http.post("/startCourseTeacherController/stopSignOfTeacher.do", { userId: this.teacherId, unitId: this.unitId}).subscribe(item => {
      })
    }
  }
  ngAfterViewInit(){
    this.navBar.backButtonClick
      = this.backButtonClick;
  }
  backButtonClick = (e: UIEvent) => {
     this.navCtrl.push(TeachingPage,{ classcourseId: this.classcourseId,unitId:this.unitId});
  };

  signend() {
    let toast = this.toastCtrl.create({
      message: '签到结束！',
      duration: 3000,//3秒后自动消失
      position: 'bottom'//位置
    });
    toast.present();//符合触发条件后立即执行显示。一定不能忘了这个
  }
  doRefresh(refresher) {
    let loading = this.loadingCtrl.create({
      content: '刷新中...',
      showBackdrop: false
    });
    loading.present();
    this.LoadList(1);
    setTimeout(() => {
      refresher.complete();
      loading.dismiss();
    }, 2000);
  }
  qrtime() {
    this.setsigntime = false;
    this.signtext = "签到进行中";
    this.http.post("/startCourseTeacherController/startSignOfTeacher.do", { userId: this.teacherId, unitId: this.unitId,minutes:this.signtime}).subscribe(item => {

    })
  }

}
